<template>
  <div class="upload-photo">
    <div class="text">
      <span class="title">寻找丢失的TA</span>
      <p class="subtitle">上传照片，我们帮您寻找</p>
    </div>
    
    <div class="upload-container">
      <div class="upload">
        <el-upload
          action="#" 
          list-type="picture-card"
          :auto-upload="false"
          :file-list="fileList"
          :limit="1"
          :on-exceed="handleExceed"
          class="custom-upload"
        >
          <template #default>
            <div class="upload-placeholder">
              <el-icon class="upload-icon"><Plus /></el-icon>
              <div class="upload-text">点击上传图片</div>
            </div>
          </template>
          
          <template #file="{ file }">
            <div class="photo-icon" v-if="dialogVisible">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt=""/>
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <el-icon><zoom-in/></el-icon>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <el-icon><Download/></el-icon>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <el-icon><Delete/></el-icon>
                </span>
              </span>
            </div>
          </template>
        </el-upload>
      </div>
      
      <div class="upload-tips">
        <p>支持 jpg、png 格式，单张图片不超过 5MB</p>
      </div>
    </div>

    <div class="btn-container">
      <el-button type="primary" class="submit-btn" @click="">
        开始寻找
      </el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
//这里上传照片的时候，想把这个文件的东西保存到fileList中再传过去，但是还没有找到方法！！
import {ref} from 'vue'
import type {UploadFile} from 'element-plus'

const dialogImageUrl = ref('')
const dialogVisible = ref(true)
const disabled = ref(false)
const fileList = ref([])
/**
 * 点击垃圾桶则可以将显示在页面上的照片让他不显示
 * @param file
 */
const handleRemove = (file: UploadFile) => {

  dialogVisible.value = false
  console.log("file.name=" + file.name)
  console.log("file.raw=" + file.raw)
  console.log("file.url=" + file.url)
}
/**
 * 点击放大镜可以让照片放大
 * @param file
 */
const handlePictureCardPreview = (file: UploadFile) => {
  dialogImageUrl.value = file.url!
  dialogVisible.value = true
}

const handleDownload = (file: UploadFile) => {
  console.log(file)
}

const handleExceed: UploadProps['onExceed'] = (files) => {
  ElMessage.warning('最多上传一张照片')
}
</script>
<style scoped>
.upload-photo {
  max-width: 900px;
  margin: 60px auto;
  padding: 40px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.text {
  text-align: center;
  margin-bottom: 40px;
}

.title {
  font-size: 32px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 12px;
  display: block;
}

.subtitle {
  font-size: 16px;
  color: #606266;
  margin-top: 8px;
}

.upload-container {
  margin: 30px 0;
}

.upload {
  width: 100%;
  min-height: 240px;

  border: 2px dashed #e4e7ed;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  background: #fafafa;
}

.upload:hover {
  border-color: #409eff;
  background: #f5f7fa;
}

.upload-placeholder {
  text-align: center;
}

.upload-icon {
  font-size: 32px;
  color: #909399;
  margin-bottom: 8px;
}

.upload-text {
  color: #606266;
  font-size: 14px;
}

.upload-tips {
  text-align: center;
  margin-top: 12px;
  color: #909399;
  font-size: 13px;
}

.btn-container {
  margin-top: 40px;
  text-align: center;
}

.submit-btn {
  width: 240px;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  border: none;
  border-radius: 24px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  background: linear-gradient(135deg, #66b1ff 0%, #409eff 100%);
}

.custom-upload :deep(.el-upload--picture-card) {
  width: 180px;
  height: 180px;
  border-radius: 8px;
}

.photo-icon {
  border-radius: 8px;
  overflow: hidden;
}
</style>
